﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Net;
using videosource;
using mjpeg;

namespace CYM_IPcap
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //測試用連結
            //http://129.78.249.81/axis-cgi/mjpg/video.cgi
            
            String source = textBox1.Text.ToString();
            //test
            Console.WriteLine(source);
            
            source = "http://" + source + "/axis-cgi/mjpg/video.cgi";
            //test
            Console.WriteLine(source);
            
            while (true)
            {

                HttpWebRequest  HWRequest = null;
                WebResponse     WResponse = null;
                byte[]          boundary = null;
                int             boundaryLen = 0;
                Stream          stream = null;

                try
                {
                    // create request
                    HWRequest = (HttpWebRequest)WebRequest.Create(source);

                    // get response
                    WResponse = HWRequest.GetResponse();

                    string ct = WResponse.ContentType;
                    if (ct.IndexOf("multipart/x-mixed-replace") == -1)
                        throw new ApplicationException("Invalid URL");

                    // get boundary
                    ASCIIEncoding encoding = new ASCIIEncoding();
                    boundary = encoding.GetBytes(ct.Substring(ct.IndexOf("boundary=", 0) + 9));
                    boundaryLen = boundary.Length;
                    Console.WriteLine(boundaryLen);

                    // get response stream
                    stream = WResponse.GetResponseStream();

                    Console.WriteLine(stream.GetType());


                    break;
                }
            }
        }
    }
}